//
//  ViewController.swift
//  wechatManager
//
//  Created by tangchunhui on 15/3/29.
//  Copyright (c) 2015年 tangchunhui. All rights reserved.
//

import Foundation
import UIKit

class ViewController :UITableViewController{
    
    var md5str:String=""
    var failureview:UIView?  //声明失败view

    var list :[String]=["自定义菜单","账号绑定","预约挂号"]
    override func viewDidLoad() {
        super.viewDidLoad()
        self.failureview = NSBundle.mainBundle().loadNibNamed("FailureView", owner: self, options: nil)[0] as? UIView
        self.failureview!.frame = self.view.frame
        self.failureview!.userInteractionEnabled = true
        var tap = UITapGestureRecognizer(target: self, action: "handleReopen")
        self.failureview!.addGestureRecognizer(tap)
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "handleReopen", name: "appWillEnterForegroundNotification", object: nil)

        //注册一个Observer 以被再次调用
        
        self.handleReopen()

    }
    override func numberOfSectionsInTableView(tableView: UITableView) -> Int {
        return 1;
    }
    override func tableView(tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return list.count
    }
    override func tableView(tableView: UITableView, cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell {
        var cell = tableView.dequeueReusableCellWithIdentifier("TableViewCell") as?  UITableViewCell
        cell?.textLabel!.text = list[indexPath.row]
        return cell!
        
    }
    override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
        var row = indexPath.row
        var text = list[row]
        if (row==0) {
            var s = ModalMenu()
            DataModal.instance().executeModal(s, withhud: true, completion:{(data:Dictionary!)->() in
                dispatch_async(dispatch_get_main_queue(), {
                   self .performSegueWithIdentifier("MenuManager", sender: self)
                })
            },failure:nil)

        } else if (text=="账号绑定" ){
                self .performSegueWithIdentifier("PatientList", sender: self)
            
        }else if (text=="预约挂号" ){
            /*
            var m = ModalMessage(md5str: self.md5str, openid: "ohR4EtyGBw9FthN2XYL4AC0o-aVE", testname: "测试", testdate: "测试时间")
            DataModal.instance().executeModal(m, withhud: true, completion:{(data:Dictionary!)->() in
            
            })
            */
            self.performSegueWithIdentifier("Reservation", sender: self)
            self.tableView.deselectRowAtIndexPath(indexPath, animated: true)
        }
    }
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        
        var vc = segue.destinationViewController
        if vc .isKindOfClass( PatientListViewController.self){
           
           (vc as! PatientListViewController).md5str = self.md5str
        }
        
    }
    func handleReopen(){
        //取当前登录用户，如果已经登录，直接跳过登录界面
        
        var array :Array<User>= DataModal.instance().queryData("User", whereString:"") as! Array<User>
        
        if (array.count>0){
            var user:User  = array[0]
            var md5str = user.md5str;
            
            var modalAccess = ModalAccess()
            DataModal.instance().executeModal(modalAccess, withhud:true ,completion:{(data:Dictionary!)->() in
                    var data1:Dictionary<String,String> = data as! Dictionary<String,String>
                //判断是否存在失败view
                dispatch_sync(dispatch_get_main_queue(), {
                    if (self.failureview!.superview != nil){
                        self.failureview!.removeFromSuperview()
                    }
                })

                
                },failure:{
                    dispatch_sync(dispatch_get_main_queue(), {
                        //失败则显示失败view
                        if (self.failureview!.superview == nil){
                           self.view .addSubview(self.failureview!)
                        }
                    })
            })
         } else {
           self.navigationController?.popViewControllerAnimated(true)
         } 
    }
    deinit {
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
}